﻿using CPPEI.ETL.DataServiceInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.ETL.DSImplement
{
    /// <summary>
    /// address:assembly;type
    /// </summary>
    public class ExtDtSerProxy : IDataServiceProxy, IDetectable
    {
        public TDataService GetSchema(DataServiceInfo info)
        {
            throw new NotImplementedException();
        }

        public TDataSource GetSchema(DataSourceInfo info)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public IData GetData(DataSourceInfo info, Dictionary<string, object> args)
        {
            string[] arr = info.Address.Split(new char[] { ';' });
            if(arr.Length != 2)
            {
                return null;
            }
            IExtDSAdapter adapter = ExtDSAdapterFactory.Create(arr[0], arr[1]);
            if(adapter == null)
            {
                return null;
            }
            System.Data.DataTable dataTable = adapter.GetData(info, args);
            return new DataTableData(dataTable);
        }

        public List<string> Enumerate(DataServiceInfo info)
        {
            throw new NotImplementedException();
        }

        #region IDetectable
        public List<TDataSource> EnumerateSources(DataServiceInfo service)
        {
            string[] arr = service.Address.Split(new char[] { ';' });
            if (arr.Length != 2)
            {
                return null;
            }

            IExtDSAdapter adapter = ExtDSAdapterFactory.Create(arr[0], arr[1]);
            if (adapter == null)
            {
                return null;
            }
            return adapter.EnumerateSources();
        }

        public List<TDataSourceParameter> EnumeratePrameters(DataSourceInfo source)
        {
            string[] arr = source.Address.Split(new char[] { ';' });
            if (arr.Length != 2)
            {
                return null;
            }

            IExtDSAdapter adapter = ExtDSAdapterFactory.Create(arr[0], arr[1]);
            if (adapter == null)
            {
                return null;
            }

            List<TDataSource> sources = adapter.EnumerateSources();
            for (int index = 0; index < sources.Count; index++)
            {
                if(string.Equals(sources[index].Value, source.DataSourceValue))
                {
                    return sources[index].Parameters;
                }
            }
            return null;
        }

        public List<TDataSourceColumn> EnumerateColumns(DataSourceInfo source)
        {
            string[] arr = source.Address.Split(new char[] { ';' });
            if (arr.Length != 2)
            {
                return null;
            }

            IExtDSAdapter adapter = ExtDSAdapterFactory.Create(arr[0], arr[1]);
            if (adapter == null)
            {
                return null;
            }

            List<TDataSource> sources = adapter.EnumerateSources();
            for (int index = 0; index < sources.Count; index++)
            {
                if (string.Equals(sources[index].Value, source.DataSourceValue))
                {
                    return sources[index].Columns;
                }
            }
            return null;
        }
        #endregion
    }
}
